home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / PipedOutputStream.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  136 lines

  1. /*
  2.  * @(#)PipedOutputStream.java    1.15 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. import java.io.*;
  18.  
  19. /**
  20.  * A piped output stream is the sending end of a communications 
  21.  * pipe. Two threads can communicate by having one thread send data 
  22.  * through a piped output stream and having the other thread read the 
  23.  * data through a piped input stream. 
  24.  *
  25.  * @author  James Gosling
  26.  * @version 1.15, 07/01/98
  27.  * @see     java.io.PipedInputStream
  28.  * @since   JDK1.0
  29.  */
  30. public
  31. class PipedOutputStream extends OutputStream {
  32.  
  33.     /* REMIND: identification of the read and write sides needs to be
  34.        more sophisticated.  Either using thread groups (but what about
  35.        pipes within a thread?) or using finalization (but it may be a
  36.        long time until the next GC). */
  37.     private PipedInputStream sink;
  38.     boolean connected = false;
  39.  
  40.     /**
  41.      * Creates a piped output stream connected to the specified piped 
  42.      * input stream. 
  43.      *
  44.      * @param      snk   The piped input stream to connect to.
  45.      * @exception  IOException  if an I/O error occurs.
  46.      * @since      JDK1.0
  47.      */
  48.     public PipedOutputStream(PipedInputStream snk)  throws IOException {
  49.     connect(snk);
  50.     }
  51.     
  52.     /**
  53.      * Creates a piped output stream that is not yet connected to a 
  54.      * piped input stream. It must be connected to a piped input stream, 
  55.      * either by the receiver or the sender, before being used. 
  56.      *
  57.      * @see     java.io.PipedInputStream#connect(java.io.PipedOutputStream)
  58.      * @see     java.io.PipedOutputStream#connect(java.io.PipedInputStream)
  59.      * @since   JDK1.0
  60.      */
  61.     public PipedOutputStream() {
  62.     }
  63.     
  64.     /**
  65.      * Connects this piped output stream to a receiver. 
  66.      *
  67.      * @param      snk   the piped output stream to connect to.
  68.      * @exception  IOException  if an I/O error occurs.
  69.      * @since      JDK1.0
  70.      */
  71.     public void connect(PipedInputStream snk) throws IOException {
  72.     if (connected || snk.connected) {
  73.         throw new IOException("Already connected");
  74.     }
  75.     sink = snk;
  76.     snk.closed = false;
  77.     snk.in = -1;
  78.     snk.out = 0;
  79.     connected = true;
  80.     }
  81.  
  82.     /**
  83.      * Writes the specified <code>byte</code> to the piped output stream.
  84.      *
  85.      * @param      b   the <code>byte</code> to be written.
  86.      * @exception  IOException  if an I/O error occurs.
  87.      * @since      JDK1.0
  88.      */
  89.     public void write(int b)  throws IOException {
  90.     sink.receive(b);
  91.     }
  92.  
  93.     /**
  94.      * Writes <code>len</code> bytes from the specified byte array 
  95.      * starting at offset <code>off</code> to this piped output stream. 
  96.      *
  97.      * @param      b     the data.
  98.      * @param      off   the start offset in the data.
  99.      * @param      len   the number of bytes to write.
  100.      * @exception  IOException  if an I/O error occurs.
  101.      * @since      JDK1.0
  102.      */
  103.     public void write(byte b[], int off, int len) throws IOException {
  104.     sink.receive(b, off, len);
  105.     }
  106.  
  107.     /**
  108.      * Flushes this output stream and forces any buffered output bytes 
  109.      * to be written out. 
  110.      * This will notify any readers that bytes are waiting in the pipe.
  111.      *
  112.      * @exception IOException if an I/O error occurs.
  113.      * @since     JDK1.0
  114.      */
  115.     public synchronized void flush() throws IOException {
  116.     if (sink != null) {
  117.             synchronized (sink) {
  118.                 sink.notifyAll();
  119.             }
  120.     }
  121.     }
  122.  
  123.     /**
  124.      * Closes this piped output stream and releases any system resources 
  125.      * associated with this stream. 
  126.      *
  127.      * @exception  IOException  if an I/O error occurs.
  128.      * @since      JDK1.0
  129.      */
  130.     public void close()  throws IOException {
  131.     if (sink != null) {
  132.         sink.receivedLast();
  133.     }
  134.     }
  135. }
  136.